const router = require('koa-router')(),//可以用require直接调用，也可以实例化调用Router = require('koa-router')    router = new Router()
      users = require('../models/users.js'),
      _     = require('lodash'),
      {JWT_KEY} = require('../lib/constant.js'),
      jwt = require('jsonwebtoken'),
      log = console.log;

router.prefix('/api/users');

router.post('/login',async(ctx,next) => {
  
  let { email,password } = ctx.request.body;

  let res = await users.login(email,password);

  if(res.code === 0){
    //generate token
    //send token to client
    //res.data = token
    res.data = await jwt.sign(_.toPlainObject(res.data),JWT_KEY);
  }


  ctx.body = res;
});



module.exports = router;
